package com.luli.controllor;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.luli.Utils.easeExcel.ExcelUtil;
import com.luli.dao.UsersDao;
import com.luli.entity.Blood;
import com.luli.entity.easeExcel.BillDO;
import com.luli.service.impl.ExcelExportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * 陆离
 * 2025-3-10
 */
@RestController
@Slf4j
@CrossOrigin
public class ExcelExportController {
    @Autowired
    private ExcelExportService excelExportService;

    @Autowired
    private UsersDao usersDao;
    // http://127.0.0.1:8081/LuLi/exportExcel
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里 URLEncoder.encode 可以防止中文乱码
            String fileName = URLEncoder.encode("复杂表格数据.xlsx", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);

        // 获取表头和数据
        List<List<String>> head = excelExportService.getHead();
        List<Map<String, Object>> data = excelExportService.getComplexData();

        // 转换数据为 List<List<Object>> 类型
        List<List<Object>> dataList = new java.util.ArrayList<>();
        for (Map<String, Object> row : data) {
            List<Object> rowData = new java.util.ArrayList<>();
            for (List<String> colHead : head) {
                String key = colHead.get(0);
                rowData.add(row.get(key));
            }
            dataList.add(rowData);
        }

        // 使用 EasyExcel 导出数据
        EasyExcel.write(response.getOutputStream())
                .head(head)
                .sheet("复杂表格数据")
                .doWrite(dataList);
    }



    /**
     * 导出
     * https://blog.csdn.net/weixin_37833693/article/details/137150585?ops_request_misc=%257B%2522request%255Fid%2522%253A%25227f4f5ba0f2ef0112b43d0b882b6cf850%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=7f4f5ba0f2ef0112b43d0b882b6cf850&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-7-137150585-null-null.142^v102^pc_search_result_base2&utm_term=springBoot%E9%9B%86%E6%88%90easeExcel&spm=1018.2226.3001.4187
     * @param response
     */
    @GetMapping("/export")
    public void exportExcel222(HttpServletResponse response) {
        try {
            ExcelUtil.setExcelResponseProp(response, "订单列表");
            List<Blood> bloodData = usersDao.getBloodData();
            EasyExcel.write(response.getOutputStream())
                    .withTemplate("./excel/ceshi.xlsx")
                    .head(BillDO.class)
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet("订单列表")
                    .doWrite(bloodData);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
